package com.lxp.base.net.listener;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.lxp.base.net.self.BaseCallBack;

import okhttp3.Interceptor;

/**
 * Created by lxp on 2017/6/4.
 */

public abstract class ProgressListener<T> implements IProgressListener, BaseCallBack<T> {

    private Gson gson = new Gson();
    private Interceptor interceptor;

    public ProgressListener(@NonNull Interceptor interceptor) {
        this.interceptor = interceptor;
    }

    @Override
    public void update(long bytesRead, long contentLength, boolean done) {
        double percent = (100 * bytesRead) / contentLength;
        this.progress(percent, bytesRead, contentLength, done);
    }

    public Interceptor getInterceptor() {
        return interceptor;
    }

    public void setInterceptor(Interceptor interceptor) {
        this.interceptor = interceptor;
    }

    @Override
    public void isSuccess(String okString, T t) {
        T obj = gson.fromJson(okString, (Class<T>) t.getClass());
        isOk(okString, obj);
    }

    @Override
    public void isFail(Throwable e) {

    }

    @Override
    public void progress(double percent, long bytesRead, long contentLength, boolean done) {

    }

    abstract void isOk(String okString, T t);

}
